//
//  ChooselabCell.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/23.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChooselabCell.h"
#include "ChooseLab.h"
@implementation ChooselabCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)loadCellArr:(NSArray *)LabArr Choose:(NSMutableArray *)chooseArr
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    _chooseArr = chooseArr;
    //计算cell的高度
    int baseNumber = (int)LabArr.count/4;
    int lastNumber = (int)LabArr.count%4;
    if (lastNumber>0) {
        baseNumber++;
    }
    
    //底层的View
    _baseView = [UIView new];
    [self.contentView addSubview:_baseView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@(55*screenW_6*baseNumber-15*screenW_6));
        make.top.equalTo(self.contentView).offset(5*screenW_6);
        make.bottom.equalTo(self.contentView).offset(0);
    }];
    
    for (int i = 0; i<LabArr.count; i++) {
        
        float x = 15*screenW_6 + ((SCREENW-75*screenW_6)/4+15*screenW_6)*(i%4);
        float y = (36*screenW_6+15*screenW_6)*(i/4);
        ChooseLab *chooseLab = [[ChooseLab alloc]initWithFrame:CGRectMake(x,y, (SCREENW-75*screenW_6)/4, 36*screenW_6) textAligment:1 font:13*screenW_6 textColor:@"#666666" text:LabArr[i]];
        [_baseView addSubview:chooseLab];
        chooseLab.tag = 10 + i;
        chooseLab.block = ^(ChooseLab *lab){
            [self removeTitle:_chooseArr];
            [self selecNowtLab:lab];
            if (_chooseCellblock) {//回调
                self.chooseCellblock(lab.text,YES);
            }
        };
        if ([_chooseArr containsObject:chooseLab.text]) {
            [chooseLab selectLab];
        }
    }
}

//改变当前选中的lab
-(void)selecNowtLab:(ChooseLab *)lab
{
    for (UIView *view in _baseView.subviews) {
        if ([view isKindOfClass:[ChooseLab class]]) {
            ChooseLab *chooselab = (ChooseLab *)view;
            if (chooselab.tag == lab.tag) {
                [chooselab selectLab];
            }else
            {
                [chooselab unSelectLab];
            }
        }
    }
}

//找出已经选中的标题 回调删除
-(void)removeTitle:(NSArray *)chooseArr{
    for (UIView *view in _baseView.subviews) {
        if ([view isKindOfClass:[ChooseLab class]]) {
            ChooseLab *chooselab = (ChooseLab *)view;
            if ([_chooseArr containsObject:chooselab.text]) {
                if (_chooseCellblock) {//回调
                    self.chooseCellblock(chooselab.text,NO);
                }
            }
        }
    }
}


@end
